﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

using BCDManager.UI.Commands;
using Microsoft.Samples.BcdSampleLib;

namespace BCDManager.UI.ViewModels
{
	public class BcdStoreViewModel : ViewModelBase
	{
        #region Fields
        private List<string> _bcdStoreEntries;
        public string _selectedBcdEntry;
        #endregion

        #region Properties        
        public List<string> BcdStoreEntries
        {
            get { return _bcdStoreEntries; }
            set 
            {
                _bcdStoreEntries = value;
                OnPropertyChanged("BcdStoreEntries");
            }
        }

        public string SelectedBcdEntry
        {
            get { return _selectedBcdEntry; }
            set
            {
                _selectedBcdEntry = value;
                OnPropertyChanged("SelectedBcdEntry");
            }
        }
        #endregion

        #region Commands

        private DelegateCommand getBcdStoreCommand;
        public ICommand GetBcdStoreCommand
        {
            get
            {
                if (getBcdStoreCommand == null)
                {
                    getBcdStoreCommand = new DelegateCommand(GetBcdStore);
                }
                return getBcdStoreCommand;
            }
        }

        private DelegateCommand createBcdEntryCommand;
        public ICommand CreateBcdEntryCommand
        {
            get
            {
                if (createBcdEntryCommand == null)
                {
                    createBcdEntryCommand = new DelegateCommand(CreateBcdEntry);
                }
                return createBcdEntryCommand;
            }
        }

        private DelegateCommand deleteBcdEntryCommand;
        public ICommand DeleteBcdEntryCommand
        {
            get
            {
                if (deleteBcdEntryCommand == null)
                {
                    deleteBcdEntryCommand = new DelegateCommand(DeleteBcdEntry, 
                                            delegate() { return !string.IsNullOrEmpty(SelectedBcdEntry); });
                }
                return deleteBcdEntryCommand;
            }
        }

        #endregion

        #region Constructor
        public BcdStoreViewModel()
		{
            GetBcdStoreCommand.Execute(null);
		}
		#endregion

        #region Commands Definitions
        private void GetBcdStore()
        {
            try
            {
                List<string> guidList =
                    BcdStore_API.EnumerateObjectsByType(Constants.BCDE_VISTA_OS_ENTRY, "");

                BcdStoreEntries = guidList;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot retrieve BCDStore Entries : " + ex.Message, 
                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void CreateBcdEntry()
        {
            try
            {
               // TODO : Perform Add
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Cannot add new BCDEntry to BCDStore. Reason(s) : {1}", ex.Message),
                   "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void DeleteBcdEntry()
        {
            try
            {
                // TODO : Perform Delete
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Cannot delete BCDEntry {0} from BCDStore. Reason(s) : {1}", null, ex.Message),
                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        #endregion
	}
}
